package com.ygq.manageGame.controller;

import com.ygq.manageGame.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@Controller
public class UploadController {

//    private static final String UPLOAD_DIR = "UploadForPhotos/";
//
//    @PostMapping("/upload/header")
//    public Result uploadHeader(@RequestParam("header") MultipartFile header) {
//        try {
//            if (!header.isEmpty()) {
//                saveFile(header, UPLOAD_DIR + "header/");
//                return Result.success();
//            } else {
//                return Result.error(500, "头像不能为空");
//            }
//        } catch (IOException e) {
//            log.error("头像上传失败", e);
//            return Result.error(500, "头像上传失败");
//        }
//    }
//
//    @PostMapping("/upload/photos")
//    public Result uploadPhotos(@RequestParam("photos") MultipartFile[] photos) {
//        try {
//            if (photos.length > 0) {
//                for (MultipartFile photo : photos) {
//                    saveFile(photo, UPLOAD_DIR + "photos/");
//                }
//                return Result.success();
//            } else {
//                return Result.error(500, "图片不能为空");
//            }
//        } catch (IOException e) {
//            log.error("图片上传失败", e);
//            return Result.error(500, "图片上传失败");
//        }
//    }
//    private void saveFile(MultipartFile file, String directory) throws IOException {
//        // 确保目录存在
//        File dir = new File(directory);
//        if (!dir.exists()) {
//            dir.mkdirs();
//        }
//
//        // 生成唯一的文件名，避免覆盖
//        String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
//        File dest = new File(dir, fileName);
//        @RequestParam("photos") MultipartFile[] photos
//        // 保存文件
//        file.transferTo(dest);
//        log.info("文件 {} 上传成功，保存为 {}", file.getOriginalFilename(), dest.getAbsolutePath());
//    }

    @PostMapping("/upload/header")
    public Result uploadHeader(@RequestParam("header")MultipartFile header) throws IOException
    {

        String path = ResourceUtils.getURL("classpath:").getPath();

        File file = new File(path + "static/images/upload/");
        if (!file.exists()) {
            file.mkdir();
        }
        if (!header.isEmpty()) {
            String originalFilename = header.getOriginalFilename();
            header.transferTo(new File(file.getAbsolutePath() + "/" + originalFilename));
            return Result.success();
        } else {
            return Result.error(500, "图片上传失败");
        }
    }

    @PostMapping("/upload/photos")
    public Result uploadPhotos(@RequestParam("photos") MultipartFile[] photos) throws IOException {

        String path = ResourceUtils.getURL("classpath:").getPath();
        File file = new File(path + "static/images/upload/");
        if (!file.exists()) {
            file.mkdir();
        }

        if (photos.length > 0) {
            for (MultipartFile photo : photos) {
                if (!photo.isEmpty()) {
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File(file.getAbsolutePath()+"/"+originalFilename));

                }
            }
            return Result.success();
        }else {
        }
        return Result.error(500, "图片上传失败");
    }

}

